package com.school.system;

import com.school.entity.Course;
import com.school.entity.Student;
import com.school.entity.Teacher;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CourseSystem {
    private List<Course> courseList;
    private List<Student> studentList;
    private List<Teacher> teacherList;
    private Scanner scanner;

    // 初始化
    public CourseSystem() {
        courseList = new ArrayList<>();
        studentList = new ArrayList<>();
        teacherList = new ArrayList<>();
        scanner = new Scanner(System.in);
        initExperiment2Data();
    }

    // 加载示例数据
    private void initExperiment2Data() {
        //课程
        Course math = new Course("C1", "高等数学", "2501", "周四 8:00");
        Course Linux = new Course("C2", "Linux", "2501", "周五 13:30");
        courseList.add(math);
        courseList.add(Linux);

        //教师+绑定课程
        Teacher wang = new Teacher("T1", "wsx", "女","TCH1");
        Teacher yu = new Teacher("T2", "yym", "男", "TCH2");
        wang.setTaughtCourse(math);
        yu.setTaughtCourse(Linux);
        teacherList.add(wang);
        teacherList.add(yu);

        //学生
        Student LZB = new Student("S1", "LZB", "男", "STU1");
        Student SKY = new Student("S2", "SKY", "男", "STU2");
        studentList.add(LZB);
        studentList.add(SKY);
    }

    public void showMainMenu() {
        System.out.println("=== 学生选课模拟系统（实验三继承改进版）===");
        System.out.println("1. 学生选课");
        System.out.println("2. 学生退课");
        System.out.println("3. 查看学生课表");
        System.out.println("4. 查看教师授课单");
        System.out.println("5. 教师开课"); // 新增选项
        System.out.println("0. 退出系统");
        System.out.print("请选择操作（输入数字）：");
    }

    public void studentSelectCourse() {
        System.out.println("\n--- 学生选课 ---");
        //输入学生ID
        System.out.print("请输入学生ID（如S1）：");
        String stuId = scanner.nextLine().trim();
        Student student = findStudentById(stuId);

        if (student == null) {
            System.out.println("错误：学生不存在！");
            return;
        }

        //显示可选课程
        System.out.println("\n可选课程列表：");
        for (Course c : courseList) {
            System.out.printf("课程ID：%s | 课程名：%s | 地点：%s | 时间：%s%n",
                    c.getId(), c.getName(), c.getLocation(), c.getTime());
        }

        //输入课程ID
        System.out.print("请输入要选择的课程ID（如C1）：");
        String courseId = scanner.nextLine().trim();
        Course course = findCourseById(courseId);

        if (course == null) {
            System.out.println("错误：课程不存在！");
            return;
        }

        //选课
        if (student.selectCourse(course)) {
            System.out.println("成功：" + student.getName() + " 已选 " + course.getName() + "！");
        } else {
            System.out.println("失败：" + student.getName() + " 已选过该课程！");
        }
    }

    //学生退课
    public void studentDropCourse() {
        System.out.println("\n--- 学生退课 ---");
        //输入学生ID
        System.out.print("请输入学生ID（如S1）：");
        String stuId = scanner.nextLine().trim();
        Student student = findStudentById(stuId);

        if (student == null) {
            System.out.println("错误：学生不存在！");
            return;
        }

        //显示已选课程
        List<Course> selected = student.getSelectedCourses();
        if (selected.isEmpty()) {
            System.out.println("提示：" + student.getName() + " 暂无已选课程！");
            return;
        }

        System.out.println("\n" + student.getName() + " 已选课程：");
        for (Course c : selected) {
            System.out.printf("课程ID：%s | 课程名：%s%n", c.getId(), c.getName());
        }

        //输入退课ID
        System.out.print("请输入要退选的课程ID：");
        String courseId = scanner.nextLine().trim();

        //退课
        if (student.dropCourse(courseId)) {
            System.out.println("成功：" + student.getName() + " 已退选课程ID " + courseId + "！");
        } else {
            System.out.println("失败：" + student.getName() + " 未选该课程！");
        }
    }

    //查看学生课表
    public void viewStudentSchedule() {
        System.out.println("\n--- 查看学生课表 ---");
        System.out.print("请输入学生ID（如S1）：");
        String stuId = scanner.nextLine().trim();
        Student student = findStudentById(stuId);

        if (student == null) {
            System.out.println("错误：学生不存在！");
            return;
        }

        student.printWorksheet();
    }

    //查看教师授课单
    public void viewTeacherCourse() {
        System.out.println("\n--- 查看教师授课单 ---");
        System.out.print("请输入教师ID（如T1）：");
        String teaId = scanner.nextLine().trim();
        Teacher teacher = findTeacherById(teaId);

        if (teacher == null) {
            System.out.println("错误：教师不存在！");
            return;
        }

        teacher.printWorksheet();
    }
    //教师开课功能
    public void addCourseByTeacher() {
        System.out.println("\n--- 教师开课 ---");
        //输入教师ID并验证
        System.out.print("请输入教师ID（如T1）：");
        String teaId = scanner.nextLine().trim();
        Teacher teacher = findTeacherById(teaId);

        if (teacher == null) {
            System.out.println("错误：教师不存在！");
            return;
        }

        //输入课程ID并检查唯一性
        System.out.print("请输入课程ID（如C3）：");
        String courseId = scanner.nextLine().trim();
        if (findCourseById(courseId) != null) {
            System.out.println("错误：课程ID已存在！");
            return;
        }

        //输入课程其他信息
        System.out.print("请输入课程名称：");
        String courseName = scanner.nextLine().trim();
        System.out.print("请输入上课地点：");
        String location = scanner.nextLine().trim();
        System.out.print("请输入上课时间（如周一 9:00）：");
        String time = scanner.nextLine().trim();

        //创建课程并关联教师
        Course newCourse = new Course(courseId, courseName, location, time);
        newCourse.setTeacherName(teacher.getName());
        courseList.add(newCourse);
        teacher.setTaughtCourse(newCourse);

        System.out.println("成功：" + teacher.getName() + " 已开设课程《" + courseName + "》！");
    }

    //根据ID查找学生
    private Student findStudentById(String stuId) {
        for (Student s : studentList) {
            if (s.getId().equals(stuId)) { // 外部包不可直接访问s.id，需用Getter
                return s;
            }
        }
        return null;
    }

    //根据ID查找课程
    private Course findCourseById(String courseId) {
        for (Course c : courseList) {
            if (c.getId().equals(courseId)) {
                return c;
            }
        }
        return null;
    }

    //根据ID查找教师
    private Teacher findTeacherById(String teaId) {
        for (Teacher t : teacherList) {
            if (t.getId().equals(teaId)) {
                return t;
            }
        }
        return null;
    }

    //启动系统：循环菜单+输入交互
    public void startSystem() {
        while (true) {
            showMainMenu();
            String choice = scanner.nextLine().trim();
            switch (choice) {
                case "1":
                    studentSelectCourse();
                    break;
                case "2":
                    studentDropCourse();
                    break;
                case "3":
                    viewStudentSchedule();
                    break;
                case "4":
                    viewTeacherCourse();
                    break;
                case "5": // 新增：调用教师开课方法
                    addCourseByTeacher();
                    break;
                case "0":
                    System.out.println("\n退出系统，感谢使用！");
                    scanner.close();
                    return;
                default:
                    System.out.println("错误：请输入0-5的有效数字！");
            }
            System.out.print("\n按回车键继续...");
            scanner.nextLine();
        }
    }

    // Getter：供测试类调用（权限控制）
    public Scanner getScanner() {
        return scanner;
    }
}